package cn.com.headfree.commworkflow.dao.impl;



import cn.com.headfree.commworkflow.dao.IWorkFlowInfoDao;
import cn.com.headfree.commworkflow.model.TWorkFlowInfo;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.common.dto.Sort;
import com.gta.edu.sdk.util.StringUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository("workFlowInfoDao")
public class WorkFlowInfoDaoImpl extends BaseDaoImpl<TWorkFlowInfo> implements IWorkFlowInfoDao {

//	@SuppressWarnings("unchecked")
//	public TWorkFlowInfo getWorkFlowInfoByBusinessId(String businessId) {
//		String hql = "from WorkFlowInfo info where info.businessId=?";
//		List<TWorkFlowInfo> list = this.find(hql, businessId);
//		if (list.isEmpty()) {
//			return null;
//		}
//		return (TWorkFlowInfo) list.get(0);
//	}
//
//	public TWorkFlowInfo getWorkFlowInfoByFlowId(String flowId) {
//		String hql = "from WorkFlowInfo wfi where wfi.businessId=(select type.id from WorkFlowType type,PersonWorkFlow flow where type.id=flow.workFlowId and flow.id=?)";
//		Object obj = this.findUnique(hql, flowId);
//		if (obj != null) {
//			return (TWorkFlowInfo) obj;
//		}
//		return null;
//	}

	/**
	 * @方法名：getWorkFlowInfosByBusinessId
	 * @功能说明：根据流程业务类型得到该业务下所有的流程信息
	 * @author fengya
	 * @date 2014-6-23 上午09:27:43
	 * @param businessId
	 * @return
	 * @return List<WorkFlowInfo>
	 */
	public List<TWorkFlowInfo> findWorkFlowsBySysCode(String sysCode) {
		return this.findAllBySQL(TWorkFlowInfo.class, "select * from t_work_flow_info wfi where wfi.sys_code=? and wfi.del_sign=0", null, true, sysCode);
	}

	public List<TWorkFlowInfo> findWorkFlowsTemplatesByOrgId(String ordId) {
		return this.findAllBySQL(TWorkFlowInfo.class, "select * from t_work_flow_info wfi where wfi.org_id=? and wfi.del_sign=0", null, true, ordId);
	}

	@Override
	public void find(Page<TWorkFlowInfo> page, Sort sort, TWorkFlowInfo workFlowInfo) {
		StringBuilder sql = new StringBuilder("select * from t_work_flow_info f where f.del_sign=0 ");
		List<Object> params = new ArrayList<Object>(1);
		if(workFlowInfo != null){
			if(!StringUtil.isNullOrEmpty(workFlowInfo.getWorkFlowName())){
				sql.append("and f.work_flow_name like ?");
				params.add("%" + workFlowInfo.getWorkFlowName().trim() + "%");
			}
			if(!StringUtil.isNullOrEmpty(workFlowInfo.getSysCode())){
				sql.append(" and f.sys_code = ?");
				params.add(workFlowInfo.getSysCode());
			}
			if(!StringUtil.isNullOrEmpty(workFlowInfo.getOrgId())){
				sql.append(" and f.org_id = ?");
				params.add(workFlowInfo.getOrgId());
			}
		}
		this.findAsPageBySql(TWorkFlowInfo.class, sql.toString(), page, sort, true, params.toArray());
	}
}
